﻿#region ... Copyright Notice ...
/*
   Copyright 2008 Tyler Jensen

	Author: Tyler Jensen    http://www.tsjensen.com/blog

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
#endregion

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Atrax.Contracts;
using Atrax.Dispatcher;

namespace Atrax.Web.Dispatcher
{
	public partial class AtraxPost : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (Request.RequestType == "POST")
			{
				ProcessRequest(Request);
			}
			else
			{
				Response.Write(ResponseFactory.GetMethodNotSupportedResponse("GET"));
				Response.End();
			}
		}

		private void ProcessRequest(HttpRequest Request)
		{
			string alldata = string.Empty;
			using (StreamReader sr = new StreamReader(this.Request.InputStream))
			{
				alldata = sr.ReadToEnd();
			}

			//convert to strings
			string[] pairs = alldata.Split('&');
			NameValueCollection form = new NameValueCollection(pairs.Length);
			foreach (string pair in pairs)
			{
				string[] keyvalue = pair.Split('=');
				if (keyvalue.Length == 2)
				{
					form.Add(keyvalue[0], HttpUtility.UrlDecode(keyvalue[1]));
				}
			}

			if (form["key"] != null
					&& form["time"] != null
					&& form["code"] != null
					&& form["id"] != null
					&& form["type"] != null
					&& form["url"] != null
					&& form["email"] != null
					&& form["priority"] != null
					&& form["query"] != null)
			{
				try
				{
					int priority = 0;
					if (!int.TryParse(Request.QueryString["priority"], out priority)) priority = 0;
					QueryRequest query = new QueryRequest()
					{
						LicenseKey = form["key"],
						ClientTimeStamp = form["time"],
						AuthorizationCode = form["code"],
						ClientQueryId = form["id"],
						QueryType = form["type"],
						CallbackUrl = form["url"],
						NotificationEmail = form["email"],
						Priority = priority,
						Query = form["query"]
					};
					RequestHandler rq = new RequestHandler();
					QueryResponse qr = rq.Execute(query);
					Response.Write(ResponseFactory.GetQueryResponse(qr));
				}
				catch (Exception e)
				{
					Response.Write(ResponseFactory.GetException(e));
				}
			}
			else
			{
				Response.Write(ResponseFactory.GetMissingValues());
			}
		}
	}
}
